package cn.kgc.Adjudicative.controller;


import cn.kgc.Adjudicative.common.Constants;
import cn.kgc.Adjudicative.common.Result;
import cn.kgc.Adjudicative.entity.DMenu;
import cn.kgc.Adjudicative.entity.DUser;
import cn.kgc.Adjudicative.entity.vo.LoginVO;
import cn.kgc.Adjudicative.service.DMenuService;
import cn.kgc.Adjudicative.service.DUserService;
import cn.kgc.Adjudicative.token.JwtToken;
import cn.kgc.Adjudicative.utils.JwtUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/d-user")
public class DUserController {

    @Resource
    private DUserService dUserService;

    @Resource
    private CaptchaController captcha;

    @Resource
    private DMenuService dMenuService;


    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO){
        if (loginVO.getUserName() == null || loginVO.getPassword() == null || loginVO.getCode() == null){
            return Result.error(Constants.CODE_500,"账号密码验证码不能为空");
        }
        Subject subject = SecurityUtils.getSubject();
        String token = JwtUtil.createJWT(loginVO.getUserName(),"back","user",1000 * 60 * 60 * 24);
        JwtToken jwtToken = new JwtToken(token,loginVO.getPassword());
        try{
            subject.login(jwtToken);
        }catch (UnknownAccountException e) {
            return Result.error(Constants.CODE_401,"账号不存在");
        } catch (IncorrectCredentialsException e) {
            return Result.error(Constants.CODE_202,"密码错误");
        }
        String kaptchaCode = captcha.getCaptcha();
        System.out.println("++++++++++"+kaptchaCode);
        String rightCode = loginVO.getCode();
         // user = null;
        if (kaptchaCode.equals(rightCode)){
            DUser user = dUserService.getUserByUsername(loginVO.getUserName());
            if (user!=null) {
                List menuList = dMenuService.lambdaQuery().like(DMenu::getMenurole,user.getRoleid()).list();
                HashMap map = new HashMap();
                map.put("user",user);
                map.put("token",token);
                map.put("menuList",menuList);
                return Result.success(Constants.CODE_200,"登录成功",map);
            }
        }else {
            return Result.error(Constants.CODE_401,"验证码输入有误");
        }
        return Result.error();
    }

    @GetMapping("/logout")
    public Result logout() {
        SecurityUtils.getSubject().logout();
        return new Result().setCode(String.valueOf(200)).setMsg("成功退出");
    }
}
